﻿/******************************************************************
** 文 件 名: Bu_BatchRec_DAL.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-21 15:53:11
** 描    述: 实现公共部分的数据访问封装，提供批次回执的数据访问类
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.DataAccess
{
	/// <summary>
    /// Bu_BatchRec数据访问类
    /// </summary>
	public partial class Bu_BatchRec_DAL : BaseDataAccess<Bu_BatchRec>
    {		
		/// <summary>
        /// 构造函数，使用指定的连接字符串
        /// </summary>
        /// <param name="connectionStringName">连接字符串名称</param>
        public Bu_BatchRec_DAL(string connectionStringName)
            : base(connectionStringName)
        {

        }

        /// <summary>
        /// 得到导入的回执
        /// </summary>
        /// <param name="arrRecID"></param>
        /// <returns></returns>
        public DataTable GetDocReceives(string[] arrRecID)
        {
            List<DbParameter> lsParameter = GetDbParametersList("ID",arrRecID);
            StringBuilder sb = new StringBuilder();
            sb.Append(@"Select A.ID,A.Code,A.ContractID,A.Amount,A.ReceiveDate,
                B.Name ContractName,C.Name ProjectName,C.SN ProjectSN
                From Bu_DocReceive A
                Inner Join BD_Contract B On A.ContractID=B.ID
                Inner Join BD_Project C On B.ProjectID=C.ID
                Where A.ID ");
            if (lsParameter.Count == 1)
            {
                sb.AppendFormat("={0}", lsParameter[0].ParameterName);
            }
            else
            {
                sb.Append(" IN (");
                foreach (DbParameter p in lsParameter)
                {
                    sb.Append(p.ParameterName);
                    sb.Append(",");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append(")");
            }
            sb.Append(" Order by A.Code");
            DbCommand command = GetDbCommand(sb.ToString());
            foreach (DbParameter p in lsParameter)
            {
                command.Parameters.Add(p);
            }
            return ExecuteDataTable(command);
        }

        /// <summary>
        /// 根据批次得到申请回执
        /// </summary>
        /// <param name="batchID"></param>
        /// <returns></returns>
        public DataTable GetDocReceiveByBatchID(string batchID)
        {
            DbParameter parameter= CreateParameter("BatchID");
            parameter.DbType = DbType.String;
            parameter.Value = batchID;
            string sql = string.Format(@"Select A.*,B.Code RecCode,B.Amount,C.Name ContractName From Bu_BatchRec A
                Inner Join Bu_DocReceive B On A.RecID=B.ID
                Inner Join BD_Contract C On B.ContractID=C.ID
                Where A.BatchID={0} Order by A.TableCode", parameter.ParameterName);
            DbCommand command = GetDbCommand(sql);
            command.Parameters.Add(parameter);
            return ExecuteDataTable(command);
        }

        /// <summary>
        /// 根据回执ID删除批次对应的回执
        /// </summary>
        /// <param name="arrRecID"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public int DeleteByRecID(string[] arrRecID, DbTransaction transaction)
        {
            string sql = @"Delete From Bu_BatchRec Where RecID in ('" + string.Join("','", arrRecID) + @"')";
            return ExecuteNonQuery(sql, transaction);
        }
    }
}
